﻿namespace Arms.Application.Modules.Orders
{
    using Arms.Application.Modules.Orders.DTOs;
    using Arms.Application.Services;
    using Arms.Domain;
    using Arms.Domain.Modules.Orders;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class OrderQueryService : ServiceBase, IOrderQueryService
    {
        public OrderQueryService(IUnitOfWork uow) : base(uow) { }

        public IEnumerable<ListItemDTO> GetOrderTypes()
        {
            return from OrderType e in Enum.GetValues(typeof(OrderType))
                   select new ListItemDTO() { ItemID = (int)e, ItemName = e.ToString() };
        }
        
        public IEnumerable<OrderDTO> GetPendingOrders()
        {
            var orders = base.UnitOfWork.Repository<Order>().GetAll(x => x.OrderState == OrderState.Open).Select(x => new OrderDTO()
            {
                OrderID = x.ID,
                OrderHeaderID = x.OrderHeaderID,
                CustomerID = x.OrderHeader.CustomerID,
                DiningRoomTableID = x.OrderHeader.DiningRoomTableID,
                OrderNumber = x.OrderNumber,
                SubTotal = x.SubTotal
            });
            return orders;
        }

        public IEnumerable<OrderHeaderDTO> GetOrderHeaders()
        {
            return base.UnitOfWork.Repository<OrderHeader>().GetAll().Select(x => new OrderHeaderDTO()
            {
                OrderHeaderID = x.ID,
                OrderHeaderNumber = x.OrderHeaderNumber
            });
        }


        public OrderHeaderDTO FindOrderHeader(int orderHeaderID)
        {
            var orderHeader = base.UnitOfWork.Repository<OrderHeader>().Find(orderHeaderID);
            return new OrderHeaderDTO()
            {
                OrderHeaderID = orderHeader.ID,
                OrderHeaderNumber = orderHeader.OrderHeaderNumber,
                OrderDate = orderHeader.OrderDate,
                OrderState = orderHeader.OrderState.ToString()
            };
        }

        public IEnumerable<OrderDTO> GetOrders(int orderHeaderID)
        {
            return base.UnitOfWork.Repository<Order>().Get(x => x.OrderHeaderID == orderHeaderID).Select(x => new OrderDTO()
            {
                OrderID = x.ID,
                OrderHeaderID = x.OrderHeaderID,
                CustomerID = x.OrderHeader.CustomerID,
                DiningRoomTableID = x.OrderHeader.DiningRoomTableID,
                OrderNumber = x.OrderNumber,
                SubTotal = x.SubTotal
            });
        }

        public OrderDTO FindOrder(int orderID)
        {
            var order = base.UnitOfWork.Repository<Order>().Find(orderID);
            return new OrderDTO()
            {
                OrderID = order.ID,
                OrderHeaderID = order.OrderHeaderID,
                CustomerID = order.OrderHeader.CustomerID,
                DiningRoomTableID = order.OrderHeader.DiningRoomTableID,
                OrderNumber = order.OrderNumber,
                OrderType = order.OrderType.ToString(),
                SubTotal = order.SubTotal
            };
        }
    }
}
